package com.itwrl.sfms.controller;

import com.itwrl.sfms.entity.TUser;
import com.itwrl.sfms.service.IUserService;
import com.itwrl.sfms.util.DESCrypto;
import com.itwrl.sfms.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public String login(){

        return "login";
    }

    @RequestMapping("userLogin")
    public String userLogin(String username,String password){
        if(username.equals("") || password.equals("")){
            return "500";
        }
        TUser tUser = userService.selectByUsername(username);
        if(tUser == null){
            return "error1";
        }
        if(!tUser.equals(password)){
            return "error2";
        }
        return "success:" + tUser.getId();


    }

    @RequestMapping("register")
    public String register(){
        return "register";
    }


    @RequestMapping("userRegister")
    @ResponseBody
    public String userRegister(TUser tUser){
        if(tUser == null){
            return "error";
        }
        TUser tUser1 = userService.selectByUsername(tUser.getUsername());
        if (tUser1 != null){
            return "error1";
        }
        tUser.setPassword(DESCrypto.encode(tUser.getPassword()));
        tUser.setType(2);
        tUser.setGmtCreate(DateUtil.getDaDate());
        int insert = userService.insert(tUser);

        if(insert == 1){
            return "success";
        }
        return "error";

    }

}
